package com.xsomnus.network_programming.netty.udpchat.config;

import com.xsomnus.network_programming.netty.udpchat.LocalUDPSocketProvider;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
 * @author @叶小夏
 * @since 2019/8/9 0009 10:06
 * - 才需学也,学需静也/非淡泊无以明志，非宁静无以致远
 */
public class UDPUtils {
    private static final String TAG = UDPUtils.class.getSimpleName();

    public static boolean send(byte[] fullProtocalBytes, int dataLen) {
        final DatagramSocket ds = LocalUDPSocketProvider.getInstance().getLocalUDPSocket();
        return UDPUtils.send(ds, fullProtocalBytes, dataLen);
    }

    private static boolean send(DatagramSocket skt, byte[] d, int dataLen) {
        if (skt != null && d != null) {
            try {
                return send(skt, new DatagramPacket(d, dataLen
//						, InetAddress.getByName(ConfigEntity.serverIP), ConfigEntity.serverUDPPort
                ));
            } catch (Exception e) {
                System.out.println(TAG + "【IMCORE】send方法中》》发送UDP数据报文时出错了：remoteIp=" + skt.getInetAddress()
                        + ", remotePort=" + skt.getPort() + ".原因是：" + e.getMessage() + e);
                return false;
            }
        } else {
            System.out.println(TAG + "【IMCORE】send方法中》》无效的参数：skt=" + skt);//
            return false;
        }
    }

    private static synchronized boolean send(DatagramSocket skt, DatagramPacket p) {
        boolean sendSucess = true;
        if ((skt != null) && (p != null)) {
            if (skt.isConnected())// 只有前面调用了connect方法后，isConnected才会返回true哦
            {
                try {
                    skt.send(p);
                } catch (Exception e) {
                    sendSucess = false;
                    System.out.println(TAG + "【IMCORE】send方法中》》发送UDP数据报文时出错了，原因是：" + e.getMessage() + e);
                }
            }
        } else {
            System.out.println(TAG + "【IMCORE】在send()UDP数据报时没有成功执行，原因是：skt==null || p == null!");
        }

        return sendSucess;
    }
}
